package br.com.ordemservico.control;


import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.swing.JOptionPane;

import br.com.ordemservico.control.conexao.Conexao;
import br.com.ordemservico.model.Cliente;
import br.com.ordemservico.model.Veiculo;
import br.com.ordemservico.view.ClienteView;
import br.com.ordemservico.view.VeiculoView;

public class VeiculoDao {
	Conexao con;
	public VeiculoDao() {
		con = new Conexao();
		
	}
	
	
public Veiculo criar(Veiculo c) {
		
		try{
			con.conectar();
			con.getTransaction().begin();
			con.getEntityManager().persist(c);
			con.getTransaction().commit();
			
		}catch(Exception e){
			System.out.println("Erro ao cadastrar Veiculo");
			e.getStackTrace();
		}finally{
			con.getEntityManager().close();
		}
		
		return c;
	}

	
	public Veiculo editar(Veiculo c) {
		
		try{
			con.conectar();
			con.getTransaction().begin();
			con.getEntityManager().merge(c);
			con.getTransaction().commit();
			
		}catch(Exception e){
			System.out.println("Erro ao editar veiculo");
			e.printStackTrace();
		}finally{
			con.getEntityManager().close();
		}
		
		return c;
	}
	
	public boolean verificarPlaca(String placa){
		Veiculo v = null;
		boolean r = false;

		try{
			con.conectar();
			con.getTransaction().begin();
			Query query = con.getEntityManager().createNativeQuery("select * from veiculo where placa = ? ", Veiculo.class).setParameter("1",placa);
			con.getTransaction().commit();
			v =    (Veiculo) query.getSingleResult();

		}catch(NoResultException e){
			r = true;
		}catch(Exception e){
			System.out.println("Erro ao pesqusiar ");
			e.printStackTrace();
		}finally{
			con.getEntityManager().close();
		}
		return r;
	}
	public Veiculo findPlaca(String placa){
		Veiculo v = null;

		try{
			con.conectar();
			con.getTransaction().begin();
			Query query = con.getEntityManager().createNativeQuery("select * from veiculo where placa = ? ", Veiculo.class).setParameter("1",placa);
			con.getTransaction().commit();
			v =    (Veiculo) query.getSingleResult();

		}catch(NoResultException e){
			v = null;
		}catch(Exception e){
			System.out.println("Erro ao pesqusiar ");
			e.printStackTrace();
		}finally{
			con.getEntityManager().close();
		}
		return v;
	}
	
	public Veiculo findId(long id){
		Veiculo c = null;
		try{
			con.conectar();
			con.getTransaction().begin();
			con.getTransaction().commit();
			c = (Veiculo) con.getEntityManager().find(Veiculo.class, id);
		}catch(Exception e){
			
		}finally{
			con.getEntityManager().close();
		}
		
		return c;
		
	}
	public boolean excluir(Veiculo c) {
		//Cliente cliente = findId(c.getId_cliente());
		boolean r = true;
		try{
			con.conectar();
			con.getTransaction().begin();
			Veiculo veiculo = con.getEntityManager().merge(c);
			con.getEntityManager().remove(veiculo);
			con.getTransaction().commit();
			
		}catch(Exception e){
			r = false;
			System.out.println("Erro ao excluir Veiculo");
			e.printStackTrace();
		}finally{
			con.getEntityManager().close();
		}
		
		return r;
		
	}
	public List<Veiculo> findAll() {
		List<Veiculo> listaVeiculo = null;
		try{
			con.conectar();
			con.getTransaction().begin();
			Query query = con.getEntityManager().createNamedQuery("Veiculo.findAll");
			con.getTransaction().commit();
			listaVeiculo =   query.getResultList();
		}catch(Exception e){
			System.out.println("Erro ao pesqusiar "+e.getStackTrace());
		}finally{
			con.getEntityManager().close();
		}
		return listaVeiculo;
	}
	public List<Veiculo> findListaPlaca(String placa) {
		List<Veiculo> listaVeiculo = null;
		try{
			con.conectar();
			con.getTransaction().begin();
			Query query = con.getEntityManager().createNativeQuery("select * from veiculo where placa like ? ", Veiculo.class).setParameter("1", "%"+placa+"%");
			con.getTransaction().commit();
			listaVeiculo =   query.getResultList();

		}catch(Exception e){
			System.out.println("Erro ao pesqusiar "+e.getStackTrace());
		}finally{
			con.getEntityManager().close();
		}
		return listaVeiculo;
	}
}
